package com.atguigu.stack;

import javax.swing.text.html.CSS;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * @author shkstart
 * @create 2021-09-20 10:23
 */
public class Stackhouzui {

//    将式子转化为集合
    public static List<String> toInfixExpressionList(String s){
        ArrayList<String> list = new ArrayList<>();
        String pijie="";
        int i=0;
        do{
            if (s.charAt(i)<47||s.charAt(i)>58){
                list.add(s.charAt(i)+"");
                i++;
            }else {
                pijie="";
                while (i<s.length()&&(s.charAt(i)>=47&&s.charAt(i)<=58)){
                    pijie+=s.charAt(i);
                    i++;
                }
                list.add(pijie);


            }
        }while (i<s.length());
        return list;
    }

    //将中缀转化为后缀
    public static List<String> parseSuffixExpreesionList(List<String> ls) {
        //创建一个符号栈
        Stack<String> s1 = new Stack<>();
        //创建一个存储数字集合
        ArrayList<String> s2 = new ArrayList<>();
        for (String item:ls){
            if (item.matches("\\d+")){
                s2.add(item);
            }else if (item.equals("(")){
                s1.push(item);
            }else if (item.equals(")")){
                while (!s1.peek().equals("(")){
                    s2.add(s1.pop());
                }
                s1.pop();
            }
            else {
                while (s1.size() != 0 && Operation1.getoper(item) <= Operation1.getoper(s1.peek())) {
                    s2.add(s1.pop());
                }
                s1.push(item);
            }
        }
        while (s1.size()!=0){
            s2.add(s1.pop());
        }
        return s2;
    }
    //后缀进行计算
    public static int calculate(List<String> ls){
        Stack<String> stack = new Stack<>();
        for (String item:ls){
            if (item.matches("\\d+")){
                stack.push(item);
            }
            else {
                int num2=Integer.parseInt(stack.pop());
                int num1=Integer.parseInt(stack.pop());
                int res=0;
               if (item.equals("+")){
                   res=num1+num2;
               }else if (item.equals("-")){
                   res=num1-num2;
               }else if (item.equals("*")){
                   res=num1*num2;
               }else if (item.equals("/")){
                   res=num1/num2;
               }else {
                   throw new RuntimeException("运算符有误！");
               }
               stack.push(String.valueOf(res));
            }
        }
        return Integer.parseInt(stack.pop());
    }
    //分隔空格
    public static List<String> getListString(String suffixExpression){
        String[] split = suffixExpression.split(" ");
        ArrayList<String> list = new ArrayList<>();
        for (String ls:split){
            list.add(ls);
        }
        return list;
    }

    public static void main(String[] args) {
//        List<String> strings = toInfixExpressionList("(50+67+8*45)-56");
//        System.out.println(strings);
//        List<String> strings1 = parseSuffixExpreesionList(strings);
//        System.out.println(strings1);
        String expression="1+((2+3)*4)-5";
        //将式子转为集合
        List<String> infixExpressionList = toInfixExpressionList(expression);
        System.out.println(infixExpressionList);
        //转化后缀表达式
        List<String>  suffixExpreesionList = parseSuffixExpreesionList(infixExpressionList);
        //进行计算
        System.out.println(suffixExpreesionList);
        int result = calculate(suffixExpreesionList);
        System.out.println(result);

        String suffixExpression="4 5 X 8 - 60 + 8 2 / +";
        List<String> rpnlist = getListString(suffixExpression);
        System.out.println("rpnlist="+rpnlist);
    }
}

//比较运算符符号的优先级
class Operation1{
 private static int ADD=1;
 private static int SUB=2;
 private static int MUL=2;
 private static int DIV=2;
 public static int getoper(String opers){
     int result=0;
     switch (opers){
         case "+":
             result=ADD;
             break;
         case "-":
             result=SUB;
             break;
         case "*":
             result=MUL;
             break;
         case "/":
             result=DIV;
             break;
//             default:
//                 System.out.println("没有该运算符");
//                 break;
                 }
                 return result;
 }

}